package com.llc.mall.rbac.application.old.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.llc.mall.common.database.qo.BasePageQo;
import com.llc.mall.common.database.util.PageUtil;
import com.llc.mall.common.database.vo.PageVO;
import com.llc.mall.rbac.infrastructure.persist.mapper.RoleMenuMapper;
import com.llc.mall.rbac.infrastructure.persist.mapper.UserRoleMapper;
import com.llc.mall.rbac.infrastructure.persist.po.RolePo;
import com.llc.mall.rbac.infrastructure.persist.mapper.RoleMapper;
import com.llc.mall.rbac.infrastructure.persist.po.RoleMenuPo;
import com.llc.mall.rbac.application.old.service.RoleService;
import com.llc.mall.rbac.interfaces.vo.RoleVo;
import org.springframework.stereotype.Service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 角色
 *
 * @author
 * @date 2020-09-17 19:15:44
 */
@Service
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private RoleMenuMapper roleMenuMapper;

    @Autowired
    private UserRoleMapper userRoleMapper;

    @Override
    public PageVO<RoleVo> page(BasePageQo pageDTO, Integer sysType, Long tenantId) {
        return PageUtil.doPage(pageDTO, () -> roleMapper.list(sysType, tenantId));
    }

    @Override
    public List<RoleVo> list(Integer sysType, Long tenantId) {
        return roleMapper.list(sysType, tenantId);
    }

    @Override
    public RoleVo getByRoleId(Long roleId) {
        RoleVo role = roleMapper.getByRoleId(roleId);
        List<RoleMenuPo> roleMenus = roleMenuMapper.getByRoleId(roleId);
//        role.setMenuIds(roleMenus.stream().filter(roleMenu -> roleMenu.menuId() != null).map(RoleMenuPo::menuId).collect(Collectors.toList()));
//        role.setMenuPermissionIds(roleMenus.stream().filter(roleMenu -> roleMenu.menuPermissionId() != null).map(RoleMenuPo::menuPermissionId).collect(Collectors.toList()));
        return role;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void save(RolePo role, List<Long> menuIds, List<Long> menuPermissionIds) {
        roleMapper.save(role);
        insertMenuAndPermission(role.roleId(), menuIds, menuPermissionIds);
    }


    @Override
    public void update(RolePo role, List<Long> menuIds, List<Long> menuPermissionIds) {
        roleMapper.update(role);
        roleMenuMapper.deleteByRoleId(role.roleId());
        insertMenuAndPermission(role.roleId(), menuIds, menuPermissionIds);
    }

    private void insertMenuAndPermission(Long roleId, List<Long> menuIds, List<Long> menuPermissionIds) {
        if (CollectionUtil.isNotEmpty(menuIds)) {
            List<RoleMenuPo> roleMenus = new ArrayList<>();
            for (Long menuId : menuIds) {
                RoleMenuPo roleMenuPo = new RoleMenuPo(null, roleId, menuId, null, null, null);
                roleMenus.add(roleMenuPo);
            }
            roleMenuMapper.insertBatch(roleMenus);
        }

        if (CollectionUtil.isNotEmpty(menuPermissionIds)) {
            List<RoleMenuPo> roleMenus = new ArrayList<>();
            for (Long permissionId : menuPermissionIds) {
                RoleMenuPo roleMenuPo = new RoleMenuPo(null, roleId, null, permissionId, null, null);
                roleMenus.add(roleMenuPo);
            }
            roleMenuMapper.insertBatch(roleMenus);
        }
    }

    @Override
    public void deleteById(Long roleId, Integer sysType) {
        roleMapper.deleteById(roleId, sysType);
        roleMenuMapper.deleteByRoleId(roleId);
        userRoleMapper.deleteByRoleId(roleId);
    }

    @Override
    public Integer getBizType(Long roleId) {
        return roleMapper.getBizType(roleId);
    }
}
